﻿/**
 * 
 * purpose: section VIEW method collection...
 * @auth: Ning
 * date: 2009-8-20
 * 
 * */
package core.section
{
	import core.ease.EaseFade;
	import core.ease.EaseFadeEvents;
	import core.ease.MotionController;
	
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.display.Sprite;

	public class SectionView extends EaseFade
	{
		//START...
		
		
		//PROPERTY...
		
		private var states:uint;
		
		protected var elements:Array;
		
						
		//CONSTRUCTOR...
		public function SectionView(_target:DisplayObject)
		{
						
			super(_target);
									
			initView();
			
			collectMotionElements();
					
		}
		
		//METHODS...
	
		//collect motion elements...
		private function collectMotionElements():void
		{
			
			var tar:Sprite = target as Sprite;
			
			elements = new Array;	
												
			var elementController:MotionController;
			
			for(var i:uint = 0;i < tar.numChildren;i++)
			{
				
				if(tar.getChildAt(i) is MovieClip && tar.getChildAt(i).name.indexOf("m_") != -1)
				{
											
					elementController = new MotionController(tar.getChildAt(i) as MovieClip);	
					
					addEelement(elementController);										
					
				}				
				
			}
			
			
		}
		
		//config element event listeners...
		private function configElementEventListeners(element:EaseFade):void
		{
			
			element.addEventListener( EaseFadeEvents.EASE_IN_OVER,elementEaseInOverHandler,false,0,true);
			
			element.addEventListener( EaseFadeEvents.EASE_OUT_OVER,elementEaseOutOverHandler,false,0,true);
			
			element.addEventListener( EaseFadeEvents.READY_FOR_DISPLAY,elementReadyDisplayHandler,false,0,true);
									
		}
		
		//check all elements are ease in...
		private function elementEaseInOverHandler(evt:EaseFadeEvents):void
		{
			
			states++;
			
			if(states >= elements.length)
			{
																								
				dispatchEvent(new EaseFadeEvents(EaseFadeEvents.EASE_IN_OVER));
				
			}									
						
		}
		
		//check all elements are ease out...
		private function elementEaseOutOverHandler(evt:EaseFadeEvents):void
		{
			
			states++;
			
			if(states >= elements.length)
			{
																			
				dispatchEvent(new EaseFadeEvents(EaseFadeEvents.EASE_OUT_OVER));
				
			}				
			
			
		}
		
		//check all element are display...
		private function elementReadyDisplayHandler(evt:EaseFadeEvents):void
		{
			
			states++;
						
			if(states >= elements.length)
			{
																						
				dispatchEvent(new EaseFadeEvents(EaseFadeEvents.READY_FOR_DISPLAY));
								
			}							
			
			
		}
								
				
		//override display...
		protected override function getReadyForDisplay():void
		{
						
			states = 0;
									
			if(states >= elements.length)
			{
																		
				dispatchEvent(new EaseFadeEvents(EaseFadeEvents.READY_FOR_DISPLAY));
				
				return;
												
			}					
			
			
			for(var i:uint = 0;i < elements.length;i++)
			{
				
				
				(elements[i] as EaseFade).display();
				
			}
			
		}
				
		//override ease in...
		public override function easeIn():void
		{
			
			states = 0;
			
			if(states >= elements.length)
			{
				
				trace("ease in over...");
																		
				dispatchEvent(new EaseFadeEvents(EaseFadeEvents.EASE_IN_OVER));
				
				return;
												
			}					
									
			for(var i:uint = 0;i < elements.length;i++)
			{
				
				(elements[i] as EaseFade).easeIn();
				
			}
			
		}
		
		//override ease out...
		public override function easeOut():void
		{
			
			states = 0;
			
			if(states >= elements.length)
			{
																		
				dispatchEvent(new EaseFadeEvents(EaseFadeEvents.EASE_OUT_OVER));
				
				return;
												
			}					
					
			for(var i:uint = 0;i < elements.length;i++)
			{
				
				(elements[i] as EaseFade).easeOut();
				
			}
			
			
		}
		
		//add element...
		public function addEelement(element:EaseFade):void
		{
			
			elements.push(element);
			
			configElementEventListeners(element);
			
		}
			
		//on target reszie...
		public function onResize():void
		{
			
			// NEED TO BE OVERRIDE...
			
		}	
	
	
		//initialize...
		protected  function initView():void
		{
			
			// TO DO...
			
		}
		
		
		
		//END
	}
}